<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/17
 * Time: 18:52
 */
session_start();
$key=$_COOKIE['taken'];
if(empty($_SESSION[$key])){
    echo '未登录，请登录<br/><a href="../admin/admin_login.php">登陆</a>';
    die();
}
//获取前台数据
$user_name=$_POST['user_name'];
$Account_Code=$_POST['Account_Code'];
$card_pwd=$_POST['card_pwd'];
if(empty($user_name)||empty($Account_Code)||empty($card_pwd)){
    echo '不能为空,<a href="open_account.php"><button>返回</button></a>';
    die();
}
//与数据库建立连接
$conn=new  mysqli('127.0.0.1','root','root','bank');
//判断该用户是否已存在
$sql="select * from accountinfo where  RealName='$user_name' and  AccountCode='$Account_Code'";
$result=$conn->query($sql);
$data=$result->fetch_assoc();
if($data==null){
    echo '不存在该账户，不能进行开卡操作<br/><a href="open_account.php">前往开户</a>';
    die();
}else{
    //获取用户id
    $account_id=$data['AccountId'];
    while (true){
        //生成卡号
        $card_no='622509823'.rand(1000,9999);
        //判断该卡号是否存在
        $sql="SELECT * FROM bankcard WHERE CardNo='$card_no';";
        $result=$conn->query($sql);
        $data=$result->fetch_assoc();
        if ($data==null){
            break;
        }
    }
    //插入数据
    $sql="INSERT INTO bankcard(CardNo,AccountId,CardPwd,CardMoney,CardState,CardTime) VALUES
('$card_no','$account_id','$card_pwd','0','0',NOW());";
    $conn->query($sql);
    if($conn->affected_rows<0){
        echo '开卡失败，请重新开卡<br/><a href="open_card.php">返回</a>';
        die();
    }else{
        echo '开卡成功<br/>';
        //展示客户卡号
        $sql="SELECT * FROM bankcard WHERE CardNo='$card_no';";
        $result=$conn->query($sql);
        $data=$result->fetch_assoc();
        echo '尊敬的'.$user_name.'您好,您的卡号为：'.$data['CardNo'];
        echo '<br/><a href="../index.php">返回首页</a>     <a href="../admin/adminlogin_action.php">管理员界面</a>';
    }

}